import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

export const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/showShareFile/:id",
    component: () => import("@/views/showShareFile/RouterIndex.vue"),
    meta: {
      hidden: true,
      title: '文件分享',
      whitelist: true,
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    meta: {
      title: "文件管理",
      elIcon: 'Files'
    },
    children: [
      {
        // 将匹配所有内容并将其放在 `$route.params.pathMatch` 下，打开文件就放在?open后面
        path: "/dashboard/:pathMatch(.*)*",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          routerViewKey: '/dashboard',
          title: "浏览",
          svgIcon: "dashboard",
          activeMenu: '/dashboard',
          affix: true
        }
      },
      {
        path: "/shareFileManagement",
        component: () => import("@/views/shareFileManagement/index.vue"),
        meta: {
          title: "分享管理",
          svgIcon: "dashboard",
          affix: true
        }
      },
    ]
  },
  { // 隐藏用
    path: "/hidden",
    component: Layouts,
    meta: {
      hidden: true,
    },
    children: [
      {
        path: "userInfo",
        component: () => import("@/views/user/userInfo/index.vue"),
        meta: {
          title: '个人信息'
        }
      }
    ]
  },
  { // 闹钟 日志
    path: "/workUtils",
    component: () => import("@/views/workUtils/RouteIndex.vue"),
    meta: {
      hidden: true,
      whitelist: true,
      title: '闹钟日志'
    },
  },
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: '/download',
    component: Layouts,
    children:[
      {
        path: "",
        component: () => import("@/views/download/index.vue"),
        meta: {
          title: '下载',
          elIcon: 'Download',
          roles: ["admin",'initAdmin','downloadPage'] // 或者在子导航中设置角色
        },
      }
    ]
  },
  {
    path: "/user",
    component: Layouts,
    redirect: "/user/userManage",
    meta: {
      title: "用户",
      elIcon: 'User',
    },
    children: [
      {
        path: "userManage",
        component: () => import("@/views/user/userManage/RouterIndex.vue"),
        name: "PagePermission",
        meta: {
          title: "用户管理",
          elIcon: 'UserFilled',
          roles: ["admin",'initAdmin'] // 或者在子导航中设置角色
        }
      },
    ]
  },
  {
    path: "/systemIntroduce",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/views/systemIntroduce/RouterIndex.vue"),
        meta: {
          title: "介绍",
          affix: true,
          elIcon: 'InfoFilled'
        }
      },
    ]
  },
  {
    path: "/ComTest",
    component: Layouts,
    children: [
      {
        path: "",
        component: () => import("@/views/ComTest/RouterIndex.vue"),
        meta: {
          title: "组件测试",
          affix: true,
          elIcon: 'InfoFilled'
        }
      },
    ]
  },
]
export const dynamicRoutesDemo: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page",
    name: "Permission",
    meta: {
      title: "权限",
      svgIcon: "lock",
      roles: ["admin", "editor"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "page",
        component: () => import("@/views/permission/page.vue"),
        name: "PagePermission",
        meta: {
          title: "页面级",
          roles: ["admin"] // 或者在子导航中设置角色
        }
      },
      {
        path: "directive",
        component: () => import("@/views/permission/directive.vue"),
        name: "DirectivePermission",
        meta: {
          title: "按钮级" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  window.location.reload()
}

export default router
